package org.frogs.algo;
import java.util.Random;

import org.frogs.algo.Point2;;

public class Player {
	/**
	 * @param args
	 */
	public static void play(FrogPool gameField) {
		
		Frog frog = gameField.getMyFrog();
		Frog enemy = gameField.getEnemyFrog();
		
		Bullet nearestBullet = Calculator.getDangerousBullet(gameField);
		
		double enemyDistance = Calculator.getFrogDistance(frog, enemy);
		
		//center of the pool
		Point2 center = new Point2(0,0);
		//direction to the center
		Point2 centerDirection = Point2.getDirection(frog.centerPoint, center);
		//direction to the Enemy
		Point2 enemyDirection = Point2.getDirection(frog.centerPoint, enemy.centerPoint);
		//direction sideways from the enemy
		Point2 enemySideDirection = Point2.getNormal(frog.centerPoint, enemy.centerPoint);
		
		//nearest bullet distance
		double distanceToNearestBulletLine = Utilities.getFrogToBulletLineDistance(frog, nearestBullet);
		double distanceToNearestBullet;
		if (nearestBullet == null) {
			distanceToNearestBullet = 1000;
		} else {
			distanceToNearestBullet = Point2.getDistanceBetweenPoints(frog.centerPoint, nearestBullet.currentPoint);
		}
		
		Point2 nextMove = frog.centerPoint;
		Point2 shot = shotDirection(enemy.centerPoint, enemyDistance, enemySideDirection);
		boolean shouldShoot = false;
		
		if (distanceToNearestBulletLine < 3.7 && distanceToNearestBullet < 35) {
			nextMove = Utilities.avoidBullet(frog, nearestBullet);
		}
		
		//FREE ZONE
		if (enemyDistance > 30) {
			//Move to center if able
			Point2 moveToCenter = Utilities.moveToDirection(frog.centerPoint, centerDirection, 2);
			double testDistance = Utilities.getPointToBulletDistance(moveToCenter, nearestBullet);
			if (testDistance > 3.7) {
				nextMove = moveToCenter;
			}
			
			//Shoot if far away
			if (enemyDistance > 35) {
				shouldShoot = true;
			}
		}
		
		//GAME ZONE
		if (enemyDistance <=30 && enemyDistance >= 21) {
			//COOL GAME ZONE
			if (enemyDistance > 25) {
				//move to the enemy
				Point2 moveIn = Utilities.moveToDirection(frog.centerPoint, enemyDirection, 2);
				double testDistance = Utilities.getPointToBulletDistance(moveIn, nearestBullet);
				if (testDistance > 3.7) {
					nextMove = moveIn;
				}
			//HOT GAME ZONE
			} else {
				//Enemy is Sitting Duck
				if (enemy.getRemReloadTime() > 2) {
					Point2 moveIn = Utilities.moveToDirection(frog.centerPoint, enemyDirection, 2);
					double testDistance = Utilities.getPointToBulletDistance(moveIn, nearestBullet);
					if (testDistance > 3.7) {
						nextMove = moveIn;
					}
				} else {
					//Too Hot
					if (enemyDistance < 23.5) {
						//move away from enemy
						Point2 moveAway = Utilities.moveToDirection(frog.centerPoint, enemyDirection, -2);
						double testDistance = Utilities.getPointToBulletDistance(moveAway, nearestBullet);
						if (testDistance > 3.7) {
							nextMove = moveAway;
						}
					//Perfect distance (25 <=> 23.5 )
					} 
					shouldShoot = true;
				}
			}
		}
		
		//DANGER ZONE
		if (enemyDistance < 21) {
			//move away from enemy
			Point2 moveAway = Utilities.moveToDirection(frog.centerPoint, enemyDirection, -2);
			double testDistance = Utilities.getPointToBulletDistance(moveAway, nearestBullet);
			if (testDistance > 3.7) {
				nextMove = moveAway;
			}
			
			//Shoot!!!
			shouldShoot = true;
		}
		
		//Do the move
		generateNextMove(nextMove, shot, shouldShoot);
	}
	
	//Generate next move
	public static void generateNextMove(Point2 nextMove, Point2 shot, boolean shouldShoot) {
		System.out.println("move " + nextMove);
		if (shouldShoot) {
			System.out.println("shoot " + shot);
		}
		else {
			System.out.println();
		}
	}
	
	//decide the direction of the shot
	public static Point2 shotDirection(Point2 enemyPos, double distance, Point2 direction) {
		Random rndNum = new Random();
		
		double rndDistance = rndNum.nextInt(5) - 2.5;
		
		Point2 shot = Utilities.moveToDirection(enemyPos, direction, rndDistance);
		
		return shot;
	}

}
